// pages/cashwithdrawal/cashwithdrawal.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getuserinfo: app.data.url + 'sheyingjia/Xmyuser1/getuserinfo',
    tixianurl: app.data.url + 'sheyingjia/Myorders/gettixian',
    user:{},
    shuju:{},
    tixian:0,
    balance:0,
    number:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync('user');
    ea.qinqiu(this.data.getuserinfo,{token:user.token},function(res){
      console.log(res)
      that.setData({ user: res.user, balance: res.user.balance});
      that.setData({shuju:res});
    })
  },
  //填写提现
  tianxie:function(e){
    var shuju = this.data.shuju;
    var tianshu = ea.getFloat(e.detail.value);
    var xianshi = ea.strTofloat(e.detail.value);
    // ea.strTofloat(e.detail.value);
    var user = this.data.user;
    if(!tianshu){
      this.setData({ balance: user.balance});
      return;
    }
    if (tianshu<1){
      wx.showToast({
        title: '最低1元起',
        icon: 'none'
      })
      this.setData({ tixian: 0 });
      return;
    }
    var value = ea.getFloat(tianshu * (1 + parseFloat(shuju.souxufei) / 100));
    value = value * parseInt(shuju.bili[1]) / parseInt(shuju.bili[0]);
    this.setData({ number: ea.getFloat(value)});
    // console.log(value);
    var balance = ea.getFloat(parseFloat(user.balance) - value);
    // console.log(balance);
    if (balance<0){
      wx.showToast({
        title: '余额不足',
        icon: 'none'
      })
      this.setData({ tixian: 0, balance: user.balance});
      return;
    }
    this.setData({ tixian: tianshu, xianshi: xianshi, balance: balance});
    
  },
  //选择全部
  tianxiequanbu:function(){
    var user = this.data.user;
    var shuju = this.data.shuju;
    var value = ea.getFloat(user.balance) * (1 - parseFloat(shuju.souxufei)/100);
    var tixian = value * parseInt(shuju.bili[0]) / parseInt(shuju.bili[1]);
    tixian = ea.getFloat(tixian);
    this.setData({ tixian: tixian, xianshi: tixian, balance: 0, number: ea.getFloat(user.balance)});
    // console.log(tixian)
  },
  //提现
  tixian:function(){
    var tixian = this.data.tixian;
    var balance = this.data.balance;
    var number = this.data.number;
    var user = wx.getStorageSync('user');
    var that = this;
    ea.qinqiu(that.data.tixianurl, { token: user.token, tixian: tixian, balance: balance, number: number},function(res){
      console.log(res);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})